package ysq.xyg.host.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.model.po.HostShop;
import ysq.xyg.host.model.vo.HostShopVo;
import ysq.xyg.host.service.HostShopService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 房产模块-门店管理controller层
 *
 * @author congcong tao
 * @date 2019-8-30 10:35:07
 */
@RestController
public class HostShopController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(HostShopController.class);
    @Resource
    private HostShopService hostShopService;

    private HostShop hostShop;

    /**
     * 分页查询
     *
     * @param hostShopVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author congcong tao
     * @date 2019-8-30 10:35:07
     */
    @BaseLog(module = "房产模块",className = "门店管理",methods = "分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/hostShop/page",method = RequestMethod.POST)
    public ReturnEntity page(HostShopVo hostShopVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = hostShopService.page(hostShopVo,pageSize,pageNum);
        return returnEntity;
    }

    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author congcong tao
     * @date 2019-8-30 10:35:07
     */
    @RequestMapping(value = "/hostShop/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(String id){
        ModelAndView view = new ModelAndView("/host/hostShopUpdateOrSave");
        if (StrUtil.isNotEmpty(id)) {
            hostShop = hostShopService.selectByPrimaryKey(id);
            view.addObject("number", hostShop.getNumber());
            view.addObject("hostShop", hostShop);
        } else {
            // 随机生成六位
            String number = RandomUtil.randomString(6);
            view.addObject("number", number);
        }
        return view;
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @author congcong tao
     * @date 2019-8-30 10:35:07
     */
    @BaseLog(module = "房产模块",className = "门店管理",methods = "删除",businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/hostShop/delete")
    public ReturnEntity delete(HttpServletRequest request,String id){
        return hostShopService.delete(id,acceptCurrentUser(request));
    }

    /**
     * 添加或修改
     *
     * @param hostShopVo Vo对象
     * @author congcong tao
     * @date 2019-8-30 10:35:07
     */
    @BaseLog(module = "房产模块",className = "门店管理",methods = "添加或修改",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/hostShop/addOrUpdate",method = RequestMethod.POST)
    public ReturnEntity delete(HttpServletRequest request,HostShopVo hostShopVo){
        return hostShopService.addOrUpdate(hostShopVo,acceptCurrentUser(request));
    }

    /**
     * 校验编号的唯一性
     * @param id
     * @param number
     * @author congcong tao
     * @date 2019/8/30 14:33
     */
    @RequestMapping(value = "/hostShop/checkNumber",method = RequestMethod.POST)
    public ReturnEntity checkNumber(String id, String number){
        String recordNumber = "";
        if (StringUtils.isNotEmpty(id)) {
            HostShop hostShop = hostShopService.selectByPrimaryKey(id);
            recordNumber = hostShop.getNumber();
        }
        HostShop hostShopSearch = new HostShop();
        hostShopSearch.setNumber(number);
        HostShop hostShop = hostShopService.selectOne(hostShopSearch);
        if (hostShop != null) {
            if (StringUtils.isNotEmpty(recordNumber) && StringUtils.equals(number, recordNumber)) {
                return new ReturnEntity(BaseConstant.FAIL, "不存在");
            }
            return new ReturnEntity();
        } else {
            return new ReturnEntity(BaseConstant.FAIL, "不存在");
        }
    }
}